/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.webapp.action;

import com.opensymphony.xwork2.Action;
import com.integrationpath.mengine.service.ArticleManager;
import com.integrationpath.mengine.service.GenericManager;
import com.integrationpath.mengine.service.UtilityManager;
import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.model.Rating;
import com.integrationpath.mengine.webapp.action.BaseAction;

import java.util.List;

public class RatingAction extends BaseAction implements Action {
    private static final String WARNING = "warning";
	private GenericManager<Rating, Long> ratingManager;
	private GenericManager<Article, Long> articleManager;
    private UtilityManager utilityManager;
    private Rating ratingEntry;
    private Long id;
    private List allRatings;

    public GenericManager<Article, Long> getArticleManager() {
		return articleManager;
	}

	public void setArticleManager(GenericManager<Article, Long> articleManager) {
		this.articleManager = articleManager;
	}

	public GenericManager<Rating, Long> getRatingManager() {
		return ratingManager;
	}

	public UtilityManager getUtilityManager() {
		return utilityManager;
	}

	public void setUtilityManager(UtilityManager utilityManager) {
		this.utilityManager = utilityManager;
	}

	public void setRatingManager(GenericManager<Rating, Long> ratingManager) {
        this.ratingManager = ratingManager;
    }

	public List getAllRatings() {
		return allRatings;
	}

	public void setAllRatings(List allRatings) {
		this.allRatings = allRatings;
	}

	public Rating getRatings() {
		return ratingEntry;
	}

	public void setRatings(Rating ratings) {
		this.ratingEntry = ratings;
	}

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }

    public String list() {
    	allRatings= ratingManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }


    public String delete() {
        ratingManager.remove(ratingEntry.getId());
        saveMessage(getText("rating.deleted"));
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            ratingEntry = ratingManager.get(id);
        } else {
            ratingEntry = new Rating();
        }
        return null;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (ratingEntry.getId() == null);

        ratingManager.save(ratingEntry);

        String key = (isNew) ? "rating.added" : "rating.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
    public String rateThis() {
    	
    	String rate = getRequest().getParameter("rating");
    	String articleId = getRequest().getParameter("article");
    	String ip = getRequest().getRemoteHost();
    	
    	Article article = new Article();
    	article.setId(new Long(articleId));
    	
    	ratingEntry = new Rating();
    	ratingEntry.setNumber(new Integer(rate));
    	ratingEntry.setArticle(article);
    	ratingEntry.setIp(ip);
    	
    	if(utilityManager.isRatedFirstTime(new Long(articleId), ip)) {
    		ratingManager.save(ratingEntry);
    		Double ratingSize = utilityManager.calculateRating(new Long(articleId));
    		Article oldArticle = articleManager.get(new Long(articleId));
    		oldArticle.setRatingsNumber(ratingSize);
    		articleManager.save(oldArticle);
    		getRequest().setAttribute("ratingSize", ratingSize);
    		return SUCCESS;
    	} 
    	else
    		return WARNING;
    }
}